// pages/my/addCustomGoods/addCustomGoods.js
import * as appConfig from '../../../app-config.js'
var app = getApp()
Page({
  data: {
    name: '',
    picUrl: '',
    detailPicUrls: ['../../../image/addPic.png'],
    isShow: true,
    sizes: [{
      name: "",
      infos: [""],
      lastId: 0
    }],
    success:false,
    agentId:'',
    accountId:'',
    versionList:[],
    isExchange:false,
    brand:'',
    brandId:'',
    carType:'',
    carType:'',
    carTypeId:'',
    carTypeColorName:'',
    carTypeColorId:'',
    firstClassName:'',
    firstClassId:'',
    secondClassName:'',
    secondClassId:'',
    specMark:true,
    specName:'',
    specId:'',
    specColorName:'',
    specColorId:'',
    isNew:true
  },
  onLoad: function(options) {
    app.getMemInfo().then(res=>{
      this.setData({
        agentId:res.data.agentId,
        accountId:res.data.id
      })
    })
    var sizes = [
    // {
    //   name: "规格名称1",
    //   infos: ["1-1", "1-2"],
    //   lastId: 1
    // }
    ]
    this.setData({
      sizes: sizes
    })
  },
  //获取商品名称
  goodNameTap: function(event) {
    var value = event.detail.value;
    this.setData({
      name: value
    })
  },
  //删除规格分类内容
  descType: function(event) {
    var dataSet = event.currentTarget.dataset;
    var sizeIndex = dataSet.sizeIndex;
    var index = dataSet.index;
    var sizes = this.data.sizes;
    var nums = sizes[sizeIndex].infos.length;
    if (nums == 1) {
      sizes[sizeIndex].infos[index] = "";
    } else {
      var infos = sizes[sizeIndex].infos
      infos.splice(index, 1);
    }

    this.setData({
      sizes: sizes,
    })
    this.getLastId(sizeIndex)
  },
  addType: function(event) {
    var dataSet = event.currentTarget.dataset;
    var sizeIndex = dataSet.sizeIndex;
    var sizes = this.data.sizes;
    sizes[sizeIndex].infos.push("");
    this.setData({
      sizes: sizes,
    })
    this.getLastId(sizeIndex)
  },
  //规格名称
  confirmCategoryNameTap: function(event) {
    var dataSet = event.currentTarget.dataset;
    var sizeIndex = dataSet.sizeIndex;
    var sizes = this.data.sizes;
    var value = event.detail.value;
    sizes[sizeIndex].name = value;
    this.setData({
      sizes: sizes
    })
  },

  //规格分类
  confirmCategoryName: function(event) {
    var dataSet = event.currentTarget.dataset;
    var sizeIndex = dataSet.sizeIndex;
    var index = dataSet.index;
    var value = event.detail.value;
    var sizes = this.data.sizes;
    sizes[sizeIndex].infos[index] = value;

    this.setData({
      sizes: sizes,
    })
    this.getLastId(sizeIndex)
  },

  getLastId: function(sizeIndex) {
    var sizes = this.data.sizes;

    var lastId = sizes[sizeIndex].infos.length - 1;
    sizes[sizeIndex].lastId = lastId;

    this.setData({
      sizes: sizes
    })
  },
  //删除规格
  deleteTypesTap: function(event) {
    var dataSet = event.currentTarget.dataset;
    var sizeIndex = dataSet.sizeIndex;
    var sizes = this.data.sizes;
    sizes.splice(sizeIndex, 1);
    this.setData({
      sizes: sizes
    })
  },
  //添加规格
  continueAdd: function() {
    var sizes = this.data.sizes;
    sizes.push({
      name: "",
      infos: [""],
      lastId: 0
    })
    this.setData({
      sizes: sizes
    })
  },
  //保存自定义上商品列表
  saveCustomGoodsTap: function() {
    var that = this
    let imgs = this.data.detailPicUrls
    if (this.data.brandId==''||this.data.brand=='') {
      wx.showToast({
        title:'请选择品牌',
        icon:'none'
      })
    }else if (this.data.name=='') {
      wx.showToast({
        title:'请输入商品名称',
        icon:'none'
      })
    }else if (this.data.picUrl=='') {
      wx.showToast({
        title:'请上传商品图片',
        icon:'none'
      })
    }else if (this.data.carTypeId==''||this.data.carType=='') {
      wx.showToast({
        title:'请选择适用车型',
        icon:'none'
      })
    }else if (this.data.carTypeColorName==''||this.data.carTypeColorId=='') {
      wx.showToast({
        title:'请选择车型颜色',
        icon:'none'
      })
    }else if (this.data.secondClassId==''||this.data.secondClassName=='') {
      wx.showToast({
        title:'请选择商品类目',
        icon:'none'
      })
    }else if (this.data.specMark) {
      if (this.data.specId==''||this.data.specName=='') {
        wx.showToast({
          title:'请选择商品规格',
          icon:'none'
        })
      }else if (this.data.specColorName==''||this.data.specColorId=='') {
        wx.showToast({
          title:'请选择商品颜色',
          icon:'none'
        })
      }else{
        this.save()
      }
    }else{    
      this.save()
      //2018-11-24废弃原上传接口
      // var url = "wx_app/custom_merchandise/saveMerchandiseInfo";
      // var data={
      //   name:this.data.name,
      //   picUrl: this.data.picUrl,
      //   productSpecifications: productSpecificationsJson,
      //   detailPicUrls: this.data.detailPicUrls,
      //   agentId:this.data.agentId,
      //   exchange:this.data.isExchange?1:0
      // }
      // console.log(data);
      // app.request(url, data, 'GET').then(res => {
      //   console.log(res)
      //   if (res.code==1) {
      //     that.setData({
      //       success:true
      //     })
      //     setTimeout(function(){
      //       wx.navigateTo({
      //         url:'../stockManage/stockManage?type=1'
      //       })
      //     },1000)
      //   }else{
      //     app.errorMsg(res.message)
      //   }
      //   // this.saveSuccess(res);
      // })
    }
  },
  //保存
  save(){
    const that = this
    let data = {
      applyTo:this.data.carType+' '+this.data.carTypeColorName,
      productType:this.data.secondClassId,//二级类目ID
      productName:this.data.name,         //商品名称
      createId:this.data.accountId,       //accountId
      agentId:this.data.agentId,          //代理商ID
      mtId:this.data.carTypeId,           //车型ID
      mtcId:this.data.carTypeColorId,     //车型的颜色ID
      brandId:this.data.brandId,          //品牌ID
      cId:this.data.specColorId,          //商品颜色ID
      versionNameId:this.data.specId,     //规格ID
      versionValues:this.data.specName,   //规格内容
      mainImgUrl:this.data.picUrl,        //主图
      imgUrl2:this.data.detailPicUrls[0]?this.data.detailPicUrls[0]:'',
      imgUrl3:this.data.detailPicUrls[1]?this.data.detailPicUrls[1]:'',
      imgUrl4:this.data.detailPicUrls[2]?this.data.detailPicUrls[2]:'',
      imgUrl5:this.data.detailPicUrls[3]?this.data.detailPicUrls[3]:'',
    }
    console.log(data)
    app.request('wx_app/custom_merchandise/saveMerchandiseInfo',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        that.setData({
          success:true
        })
        setTimeout(function(){
          wx.switchTab({
            // url:'../stockManage/stockManage?type=1'
            url:'../my'
          })
        },1000)
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  //上传商品詳情圖片
  bindUploadImg(){  
    let that = this
    wx.chooseImage({
      count:5,
      success: function(res) {
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        let detailPicUrls = []
        for (var i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url:appConfig.apiBase+'wx_app/image/upload',
            filePath:tempFilePaths[i],
            name:'image',
            success(res){ 
              var str = res.data.split(',')
              let code = str[0].split(':')[1]
              if (code == 1) {
                let length = str[3].length - 3
                let url = str[3].substring(13,length)
                detailPicUrls.push(url)
                console.log(detailPicUrls)
                that.setData({
                  detailPicUrls
                })
              }else{
                app.errorMsg('上传图片失败')
              }
            }
          })
        }
      }
    })
  },
  //上传商品图片
  bindUploadPic(){
    let that = this
    wx.chooseImage({
      success(res){
        var tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url:appConfig.apiBase+'wx_app/image/upload',
          filePath:tempFilePaths[0],
          name:'image',
          success(res){
            console.log(res)
            var str = res.data.split(',')
            let code = str[0].split(':')[1]
            if (code == 1) {
              let length = str[3].length - 3
              let url = str[3].substring(13,length)
              that.setData({
                picUrl:url
              })
            }else{
              app.errorMsg(res.message)
            }
          },
          error(res){
            console.log(res)
          }
        })
      }
    })
  },
  addSpec(){
    wx.navigateTo({
      url:'chooseSpec'
    })
  },
  deleteSpec(e){
    let id = e.currentTarget.dataset.id
    console.log(id)
    let versionList = this.data.versionList
    versionList.forEach((item,index)=>{
      if (item.specId==id) {
        versionList.splice(index,1)
      }
    })
    this.setData({
      versionList
    })
  },
  switchChange: function (e){
    console.log(e.detail.value)
    this.setData({
      isExchange:e.detail.value
    })
  },
  bindSelect(e){
    let type = e.currentTarget.dataset.type
    if (type!=0) {
      if (this.data.brandId=='') {
        app.errorMsg('请选择品牌')
      }else{
        if (type==3) {
          if (this.data.secondClassId=='') {
            app.errorMsg('请选择商品类目')
          }else{
            wx.navigateTo({
              url:'specSelect?type='+type+'&id='+this.data.secondClassId+'&isOld=0'
            })
          }
        }else{
          wx.navigateTo({
            url:'specSelect?type='+type+'&id='+this.data.brandId+'&isOld=0'
          })
        } 
      }
    }else{
      wx.navigateTo({
        url:'specSelect?type='+type
      })
    }
  }
})

